<template>
	<view class="list_main" :style="{'--color':vuex_config.themeColor1}" v-if="dataList?true:false">
		<view class="list_item" v-for="(item,index) in dataList" :key="index"
			:style="{'background-image':'url('+ item.img +')'}">
			<view class="share">
				<view class="share_title" @click="getQr(item)">分享使用</view>
			</view>
			<view class="cont_box">
				<view class="cont_item">
					<view class="name">{{item.name}}</view>
					<view class="tip">
						<text class="reset">剩余数量：{{(item.num - item.record_num) || 0}}张</text>
						<text>已使用：{{item.record_num || 0}}张</text>
					</view>
				</view>
			</view>
			<view class="record_list">
				<view class="record_title" @click="seeRecord(item)">领取记录</view>
			</view>
		</view>
		<u-popup v-model="showType" mode="center" width="70%">
			<!-- #ifndef MP-ALIPAY || APP-PLUS -->
			<image class="qr_img" :src="qrCodeUrl" mode="widthFix" show-menu-by-longpress></image>
			<!-- #endif -->
			<!-- #ifdef MP-ALIPAY || APP-PLUS -->
			<image class="qr_img" :src="qrCodeUrl" mode="widthFix"></image>
			<!-- #endif -->
		</u-popup>
		
		<view class="none" v-if="!dataList[0]">
			暂无资源~
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showType: false,
				qrCodeUrl: '',
				dataList: null
			};
		},
		onLoad() {
			this.$initPage()
		},
		methods: {
			$initPage() {
				this.$resPost('GetUsersCardInfoList').then(({
					data
				}) => {
					this.dataList = [].concat(data)
				})
			},
			getQr(item) {
				if(item.num <= item.record_num){
					this.$toast('数量不足')
					return
				}
				this.$loading()
				let content = `/package/common/conciergeCard/index?cardId=${item.pk_id}`
				let type = 1
				// #ifdef H5
				type = 2
				// #endif
				if(this.vuex_user.type == '2' || this.vuex_user.type == '4'){
					content += `&superior=${this.vuex_user.pk_id}`
				}
				let params = {
					type: type,
					width: 50,
					content: encodeURIComponent(content),
					appid: this.vuex_business.appid
				}
				this.$resPost('GetGenerateQRCode', {
					...params
				}, {}, {
					blob: true
				}).then((res) => {
					// #ifdef MP-BAIDU
					// 傻逼百度小程序不支持 uni.arrayBufferToBase64 并且本身也没有arrayBuffer转base64的api  所以需要写入文件后转换成url显示
					const fsm = swan.getFileSystemManager();
					const FILE_BASE_NAME = new Date().getTime();
					const filePath = swan.env.USER_DATA_PATH + '/' + FILE_BASE_NAME + '.png'
					fsm.writeFile({
						filePath,
						data: res,
						success() {
							that.qrCodeUrl = filePath
							this.showType = true
							uni.hideLoading()
						},
						fail() {
							uni.hideLoading()
							this.$toast('生成失败，请重新刷新')
						}
					})
					return
					// #endif 

					// #ifndef  MP-ALIPAY ||  MP-BAIDU	
					this.qrCodeUrl = 'data:image/jpeg;base64,' + uni.arrayBufferToBase64(res || []);
					// #endif 

					// #ifdef  MP-ALIPAY	
					this.qrCodeUrl = 'data:image/jpeg;base64,' + my.arrayBufferToBase64(res || [])
					// #endif
					this.showType = true
					this.$hide();
				})
			},
			seeRecord(item) {
				this.$linkTo(`/package/common/conciergeCard/recordList?id=${item.pk_id}`)
			},
		},
	};
</script>
<style>
	page {
		background-color: #fff;
	}
</style>
<style scoped lang="scss">
	.list_main {
		padding: 24rpx 30rpx 0;

		.list_item {
			margin-bottom: 20rpx;
			padding: 32rpx 30rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			background-size: cover;
			height: 414rpx;

			.share {
				display: flex;
				flex-direction: row-reverse;

				.share_title {
					color: #fff;
					font-size: 32rpx;
					background-color: var(--color);
					padding: 4rpx 20rpx;
					border-radius: 20rpx;
				}
			}

			.cont_box {
				padding-right: 32rpx;
				display: flex;
				flex-direction: row-reverse;

				.cont_item {
					max-width: 478rpx;
					text-align: center;
					color: var(--color);

					.name {
						font-size: 60rpx;
						font-weight: bold;
					}

					.tip {
						margin-top: 18rpx;
					}

					.reset {
						margin-right: 20rpx;
					}
				}
			}

			.record_list {
				margin-top: 10rpx;
				color: var(--color);
				display: flex;
				flex-direction: row-reverse;

				.record_title {
					font-weight: bold;
					text-decoration: underline;
					letter-spacing: 2rpx;
				}
			}
		}
	}

	.qr_img {
		width: 100%;
	}
	.none{
		text-align: center;
		padding-top: 150rpx;
		color: #999;
	}
</style>
